package com.test.common.concurrency;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapDemo {
		//下面三个场景都使用ConcurrentHashMap:
		//1、多线程添加或删除。2、遍历的时候删除。3、多线程遍历的时候删除数据
		//注意ConcurrentHashMap多线程操作不能保证数据同步，此时可以加同步代码块进行同步操作
	//hashmap不能边加边删
	 public static void main3(String[] args) {
	        Map<String, Integer> map = new HashMap<>();
	        map.put("a",1);
	        map.put("b",1);
	        map.put("c",1);
	        for (Map.Entry<String, Integer> entry : map.entrySet()) {
	            map.remove(entry.getKey());
	        }
	        System.out.println(map.size());
	    }
	 //ConcurrentHashMap支持边加边删
	 public static void main4(String[] args) {
//       Map<String, Integer> map = new HashMap<>();
       Map<String, Integer> map = new ConcurrentHashMap<>();
       map.put("a",1);
       map.put("b",1);
       map.put("c",1);
       for (Map.Entry<String, Integer> entry : map.entrySet()) {
           map.remove(entry.getKey());
       }
       System.out.println(map.size());
   }
	 
	public static void main1(String[] args) {
        final Map<String, Integer> count = new ConcurrentHashMap<>(); //只能保证对容器的操作是没问题的，但是不能保证业务是没有问题的，因为是复和操作且并发执行。
//        final Map<String, Integer> count = new HashMap<>();
//        final Map<String, Integer> count = new Hashtable<>();
        count.put("count",0);
        Runnable task = new Runnable(){
            @Override
            public void run() {
                int value;
                for (int i = 0; i < 2000; i++) {
                    value = count.get("count");
                    count.put("count",value + 1);
                }
            }
        };
        new Thread(task).start();
//        new Thread(task).start();
//        new Thread(task).start();
        try {
            Thread.sleep(1000l);
            System.out.println(count);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
	//加同步代码块
	public static void main(String[] args) {
//        final Map<String, Integer> count = new ConcurrentHashMap<>();
        final Map<String, Integer> count = new HashMap<>();
//        final Map<String, Integer> count = new Hashtable<>();
        count.put("count",0);
        Runnable task = new Runnable(){
            @Override
            public void run() {
                synchronized (count){
                    int value;
                    for (int i = 0; i < 2000; i++) {
                        value = count.get("count");
                        count.put("count",value + 1);
                    }
                }

            }
        };
        new Thread(task).start();
        new Thread(task).start();
        new Thread(task).start();
        try {
            Thread.sleep(1000l);
            System.out.println(count);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
	//多线程删除
	public static void main5(String[] args) {
//      final Map<String, Integer> count = new HashMap<>();
      final Map<String, Integer> count = new ConcurrentHashMap<>();
      //final Hashtable<String, Integer> count = new Hashtable<>();
      for (int i = 0; i < 2000; i++) {
          count.put("count" + i, 1);
      }
      Runnable task1 = new Runnable() {
          @Override
          public void run() {
              for (int i = 0; i < 500; i++) {
                  count.remove("count" + i);
              }
          }
      };
      Runnable task2 = new Runnable() {
          @Override
          public void run() {
              for (int i = 1000; i < 1500; i++) {
                  count.remove("count" + i);
              }
          }
      };
      new Thread(task1).start();
      new Thread(task2).start();
      try {
          Thread.sleep(1000l);
          System.out.println(count.size());
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
}
